﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thor.Common;
using Thor.Configuration;

namespace Thor.Common
{
    public class ProviderHelper
    {
        #region fields

        private static ConfigurationSettings _ConfigSettings = new ConfigurationSettings();
        private static string _ConnectionStringKey = ConfigurationSettings.ConnectionStringKey;
        private static string _ConnectionStringValue = ConfigurationSettings.ConnectionStringValue;
        private static string _ContainerAddressKey = ConfigurationSettings.ContainerAddressKey;
        private static string _ContainerAddressValue = ConfigurationSettings.ContainerAddressValue;
        private static string _RepositoryAssembly = AppDomain.CurrentDomain.BaseDirectory + ConfigurationSettings.AssemblyRepositoryAssembly;
        private static string _RepositoryType = ConfigurationSettings.AssemblyRepositoryType;
        private static string _CalendarProviderBaseClassName = ConfigurationSettings.CalendarProviderInterfaceName;

        #endregion

        #region constructors

        private ProviderHelper() { }

        #endregion

        #region properties

        private static ConfigurationSettings ConfigurationSettings
        {
            get
            {
                if (_ConfigSettings == null)
                    _ConfigSettings = new ConfigurationSettings();

                return _ConfigSettings;
            }
        }

        #endregion

        #region methods

        private static Dictionary<string, object> GetRepositoryParameters()
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add(_ConnectionStringKey, _ConnectionStringValue);
            parameters.Add(_ContainerAddressKey, _ContainerAddressValue);
            return parameters;
        }

        private static IProviderRepository GetRepository(Dictionary<string, object> parameters)
        {
            return ProviderManager.GetRepository(_RepositoryAssembly, _RepositoryType, parameters);
        }

        public static ProviderManager GetManager()
        {
            Dictionary<string, object> parameters = GetRepositoryParameters();
            IProviderRepository repository = GetRepository(parameters);
            ProviderManager manager = new ProviderManager(repository, _CalendarProviderBaseClassName);
            return manager;
        }

        #endregion
    }
}